<?php
namespace app\admin\controller;

use think\Controller;
use phpexcel\Reader;
use think\Db;;

class LoadUser extends Controller
{
    /*
    从xls表格导入数据
     */
    public function index() {
        if (!empty($_GET))
        {
            $fileName = './xls/'. $_GET['file']. '.xls';
            $tableName = $_GET['table'];
            $user = Reader::getInfo($fileName, 0);
            foreach ($user as $key => $value) {
                foreach ($value as $k => $v) {
                    $name = (string) $v;
                    $sql = 'insert into '. $tableName. ' (u_name) values (' . '\'' . $name . '\'' . ')';
//                    echo $sql;
                    Db::execute($sql);
                }
            }
            echo 'OK';
        }
    }
    public function importUser() {
//        $fileName = 'zombie.xls';
        $user = Reader::getInfo($fileName, 0);
        foreach ($user as $key => $value) {
            foreach ($value as $k => $v) {
                $name = (string) $v;
                Db::execute('insert into weibo_zombie (u_name) values (' . '\'' . $name . '\'' . ')');
            }
        }
        echo 'OK';
    }
    public function importGold() {
//        $fileName = 'gold.xls';
        $user = Reader::getInfo($fileName, 0);
        foreach ($user as $key => $value) {
            foreach ($value as $k => $v) {
                $name = (string) $v;
                Db::execute('insert into weibo_goldleafwine (u_name) values (' . '\'' . $name . '\'' . ')');
            }
        }
    echo 'OK';
    }
    public function import() {
//        $fileNme = 'weibo_user.xls';
        $userInfo = Db::query("SELECT * FROM `weibo_user` ORDER BY `id`");
        Reader::importTmp($userInfo);
        echo 'OK';
    }
}